Skip to content

docs: add parsing request#1519

Merged
jannyHou merged 1 commit into
masterfrom
doc/validation
Jul 16, 2018
Merged

docs: add parsing request#1519
jannyHou merged 1 commit into
masterfrom
doc/validation

Conversation

@jannyHou

@jannyHou jannyHou commented Jul 11, 2018

Copy link
Copy Markdown
Contributor

Connect to #1489

The story only contains validation doc, while I realized the doc for action parsing request is missing, so I added it and also the coercion doc.

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

@jannyHou jannyHou self-assigned this Jul 11, 2018

@bajtos bajtos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

I think there may be few places with incorrect grammar, I'll let somebody more fluent in English to review the language.

@shimks shimks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall content looks fab 🤩
🧤It's time for a grammar shake down, I hope you're ready

Comment thread docs/site/Parsing-requests.md Outdated
rest decorators.

```ts
class TodoController() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: this.todoRepo is not specified in this TS class

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No parenthesis

Comment thread docs/site/Parsing-requests.md Outdated
```

An operation specification will be generated in-memory to describe it, and raw
datas are parsed from request according to the specification. For example the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data does not have a plural form

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean For example, if the first parameter ...?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the example code above, rephrased it to:

In the example
above, the first parameter is from source path, then its value will be parsed
from a request's path.

Comment thread docs/site/Parsing-requests.md Outdated

### Coercion

The parameters parsed from path, header, query of a http request are always in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path, header and query (American way of using and)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also might be good to provide context that it's node.js's request parameters being in string format

Comment thread docs/site/Parsing-requests.md Outdated
need to be converted to its corresponding JavaScript runtime type, which is
inferred from its parameter specification.

Give the example operation `replaceTodo` in section

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take instead of Give might be a better choice of word

Comment thread docs/site/Parsing-requests.md Outdated
inferred from its parameter specification.

Give the example operation `replaceTodo` in section
[parsing raw data](#parsing-raw-data), which takes in a number `id` as the first

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no comma here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combined with the comment above, do you mean rephrase it to:

Take the example operation replaceTodo in section
parsing raw data which takes in a number id as the first
input, coercion means users don't need to do a type conversion by themselves in
the controller function like below:

?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, please put the comma back in. 🙇

Comment thread docs/site/Parsing-requests.md Outdated

_See [model](Model.md) to know more details about how to decorate a model class_

When the `PUT` method `/todo/{id}` get called, the `todo` instance from request

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gets

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the request body

Comment thread docs/site/Parsing-requests.md Outdated
When the `PUT` method `/todo/{id}` get called, the `todo` instance from request
body will be validated with a well defined specification.

Section [RequestBody Decorator](Decorators.md#requestbody-decorator) has a very

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Section on requestBody Decorator

Comment thread docs/site/Parsing-requests.md Outdated

Section [RequestBody Decorator](Decorators.md#requestbody-decorator) has a very
detailed explanation of the various ways to provide a request body
specification. It would be a redundant to repeat it here. So make sure you read

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the last two sentences

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I do want to emphasize "make sure you read that section", I deleted the fist sentence.

@shimks shimks Jul 12, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this:
Validation of model objects is heavily dependent on its OpenAPI Schema defined in/by @requestBody decorator. Please refer to the documentation on [@requestBody decorator](link) to get a comprehensive idea of defining custom validation rules for your models.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool 😎 !

Comment thread docs/site/Parsing-requests.md Outdated

#### Localizing errors

A body data may contain multiple invalid things, like missing required field,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A body data could break multiple validation rules, ... things is I think too informal here.

Comment thread docs/site/Sequence.md
@@ -229,7 +229,7 @@ How to get query string param values.

### Parsing Requests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to have a subpage for this section, we should get rid of its mention entirely here.
Maybe we can also consider adding a link

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

important catch! thx

Comment thread docs/site/Parsing-requests.md Outdated

An operation specification will be generated in-memory to describe it, and raw
data are parsed from request according to the specification. In the example
above, the first parameter is from source `path`, then its value will be parsed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path, so its value will be parsed from a request's path

Comment thread docs/site/Parsing-requests.md Outdated
inferred from its parameter specification.

Give the example operation `replaceTodo` in section
[parsing raw data](#parsing-raw-data), which takes in a number `id` as the first

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, please put the comma back in. 🙇

@shimks shimks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
There's a couple of outlying comments I've made that were collapsed, so please check them out as well~~

Comment thread docs/site/Parsing-requests.md Outdated
}
```

An operation specification will be generated in-memory to describe it, and raw

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: An OpenAPI operation specification

Comment thread docs/site/Parsing-requests.md Outdated
```

An operation specification will be generated in-memory to describe it, and raw
data are parsed from request according to the specification. In the example

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another nitpick: and raw data are parsed from request -> and raw data is parsed from the request

Comment thread docs/site/Parsing-requests.md Outdated

You can specify a parameter's type by calling shortcut decorators of `@param`
like `@param.query.integer()`. A list of available shortcuts can be found in the
[api documents](https://apidocs.strongloop.com/@loopback%2fdocs/openapi-v3.html#param).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: api documents -> API Docs


Here are our default validation rules for each type:

- number: validated by `isNaN(Number(data))`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I suggest to either remove . for each line of the list or add it to be consistent

@jannyHou jannyHou Jul 13, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bajtos hmm...I added it for each line....which line did you find not inconsistent?

Comment thread docs/site/Parsing-requests.md Outdated
#### Request Body

The data from request body is validated against its OpenAPI schema
specification. We use module [AJV](https://github.com/epoberezkin/ajv) to

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: module AJV -> AJV module

```ts
import {Todo} from './models';

...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nitpick: can we wrap this code block with the controller class declaration?

import {Todo} from './models';

class test {
  ...
  @put('/todos/{id}')
  async replaceTodo(
    @param.path.number('id') id: number,
    @requestBody() todo: Todo,
  ): Promise<boolean> {
    return await this.todoRepo.replaceById(id, todo);
  }
  ...
}

it just didn't render properly for me in VS code :P

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are too many irrelevant code in the class definition which distract users when reading :( that's why I deleted them.

it just didn't render properly for me in VS code

Mine looks good 🤔 :
screen shot 2018-07-13 at 3 11 18 pm

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably has to do with the ...
Commenting those out should do it

Comment thread docs/site/Parsing-requests.md Outdated

{% include note.html title="Model documentation" content="See [model](Model.md) to know more details about how to decorate a model class" %}

When the `PUT` method `/todo/{id}` gets called, the `todo` instance from the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: 'PUT' method on path '/todo/{id}'

Comment thread docs/site/Parsing-requests.md Outdated
- If a model property's type refers to another model, make sure it is also
decorated with `@model` decorator.

- By the API first approach, you can also provide the request body specification

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: By the API first approach -> If you're using API first development approach

@b-admike b-admike left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, just minor nitpicks; great work!

Comment thread docs/site/Parsing-requests.md Outdated
before it can be used as seen below:
For example, the operation `replaceTodo` in section
[parsing raw data](#parsing-raw-data) takes in a number `id` as the first input,
without coercion,`id` would have to be manually cast into the number type before

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as the first input. Without coercion, id would have to ...

```ts
import {Todo} from './models';

...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably has to do with the ...
Commenting those out should do it

@b-admike b-admike left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@jannyHou
jannyHou merged commit 6560f25 into master Jul 16, 2018
@jannyHou
jannyHou deleted the doc/validation branch July 16, 2018 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants